package com.zk.spring5.aop_annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author ZK
 * @Description
 * @time 2021/5/4 22:23
 * @note 增强的类
 */
@Component
@Aspect
@Order(1)
public class UserProxy {
    // 公共切入点抽取
    @Pointcut(value = "execution(* com.zk.spring5.aop_annotation.User.add(..))")
    public void pointCut() {
    }
    
    // 前置通知
    @Before(value = "pointCut()")
    public void before() {
        System.out.println("before....(UserProxy at order 1)");
    }

    // 最终通知。不管有无异常都执行
    @After(value = "pointCut()")
    public void after() {
        System.out.println("after...");
    }

    // 后置通知（返回通知）。在返回返回值之后才执行的
    @AfterReturning(value = "pointCut()")
    public void afterReturn() {
        System.out.println("AfterReturning...");
    }

    // 异常通知。即发生异常才会执行
    @AfterThrowing(value = "pointCut()")
    public void afterThrowing() {
        System.out.println("AfterThrowing...");
    }

    // 环绕通知。
    @Around(value = "pointCut()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 环绕之前做的事
        System.out.println("before Around...");

        // 被增强的方法
        proceedingJoinPoint.proceed();

        // 环绕之后做的事
        System.out.println("After Around...");
    }

    @AfterReturning(value = "execution(* com.zk.spring5.aop_annotation.User.getBook(..))", returning = "res")
    public Book afterReturning(Object res) {
        Book book = (Book) res;
        book.setName("zzzzzzzzzzzzzzzzzzzzzzzzzz");
        return book;
    }
}
